package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjServicePageIcon;
import com.zb.mapper.ZjServicePageIconMapper;
import com.zb.service.ZjServicePageIconService;
import com.zb.vo.ServicePageIconVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统服务页面图标表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-31
 */
@Service
public class ZjServicePageIconServiceImpl extends ServiceImpl<ZjServicePageIconMapper, ZjServicePageIcon> implements ZjServicePageIconService {

    // 保存、删除图标
    @Override
    public void save(Long custAccId, Long servicePageId, List<ServicePageIconVo> pageIcons, List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            this.removeByIds(ids);
        }
        if (CollectionUtil.isEmpty(pageIcons)) {
            return;
        }
        List<ZjServicePageIcon> adds = new ArrayList<>();
        List<ZjServicePageIcon> updates = new ArrayList<>();
        ZjServicePageIcon icon = null;
        for (ServicePageIconVo vo : pageIcons) {
            icon = new ZjServicePageIcon();
            BeanUtil.copyProperties(vo, icon);
            if (null != icon.getId()) {
                updates.add(icon);
                continue;
            }
            icon.setId(IdWorker.getId());
            icon.setCustAccId(custAccId);
            icon.setServicePageId(servicePageId);
            adds.add(icon);

        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 根据服务页面ID获取图标信息
    @Override
    public List<ZjServicePageIcon> getByServicePageId(Long servicePageId) {
        QueryWrapper<ZjServicePageIcon> wrapper = new QueryWrapper<>();
        wrapper.select("id", "image", "is_select AS `select`");
        wrapper.eq("service_page_id", servicePageId);
        return this.list(wrapper);
    }
}
